package DAO;

import Utils.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class UserDAO
{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public User Get_UserByUserName(String UserName) throws Exception
    {
        User result = null;
        String sql = "Select * from User Where username = ?";
        result = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),UserName);
        return result;
    }
    public User Get_UserByUserId(String UserId) throws Exception
    {
        User result = null;
        String sql = "Select * from User Where UserId = ?";
        result = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),UserId);
        return result;
    }
    //添加用户
    public void Add_User(User user) throws Exception
    {
        String sql = "Insert into User values(?,?,?,?)";

        Object[] args = new Object[]{user.getUserID(),user.getUsername(),user.getPassword(),user.getIs_admin()};

        jdbcTemplate.update(sql,args);
    }
    //修改密码
    public void Update_User(String UserId,String password)throws Exception
    {
        String sql = "Update User Set password = ? where userId = ?";
        Object[] args = new Object[]{password,UserId};
        jdbcTemplate.update(sql,args);
    }
}
